package com.starlinkdt.service.system.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.starlinkdt.common.domain.system.entity.OaiReqLog;
import com.starlinkdt.common.domain.system.vo.OaiReqLogVo;
import com.starlinkdt.common.mapper.system.OaiReqLogMapper;
import com.starlinkdt.common.utils.StringUtils;
import com.starlinkdt.service.system.IOaiPermService;
import com.starlinkdt.service.system.IOaiReqLogService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 请求日志服务接口实现类
 */
@Slf4j
@Service
public class OaiReqLogServiceImpl extends ServiceImpl<OaiReqLogMapper, OaiReqLog> implements IOaiReqLogService {
    
    @Autowired
    private OaiReqLogMapper oaiReqLogMapper;

    /**
     * 查询请求日志列表
     * @param vo 请求日志查询条件
     * @return 请求日志列表
     */
    @Override
    public List<OaiReqLog> list(OaiReqLogVo vo) {
        return oaiReqLogMapper.selectList(buildQueryWrapper(vo));
    }

    /**
     * 根据请求ID查询请求日志
     * @param  id 请求ID
     * @return 请求日志
     */
    @Override
    public OaiReqLog getById(Long id) {
        return oaiReqLogMapper.selectById(id);
    }

    /**
     * 新增请求日志
     * @param oaiReqLog 请求日志
     */
    @Override
    public void add(OaiReqLog oaiReqLog) {
        oaiReqLogMapper.insert(oaiReqLog);
    }

    /**
     * 构建查询条件
     * @param query 查询条件
     * @return 查询条件
     */
    private LambdaQueryWrapper<OaiReqLog> buildQueryWrapper(OaiReqLogVo query) {
        LambdaQueryWrapper<OaiReqLog> lqw = Wrappers.lambdaQuery();
        lqw.like(StringUtils.isNotBlank(query.getReqId()), OaiReqLog::getReqId, query.getReqId());
        lqw.like(StringUtils.isNotBlank(query.getAppId()), OaiReqLog::getAppId, query.getAppId());
        lqw.like(StringUtils.isNotBlank(query.getReqUri()), OaiReqLog::getReqUri, query.getReqUri());
        lqw.like(StringUtils.isNotBlank(query.getReqMethod()), OaiReqLog::getReqMethod, query.getReqMethod());
        lqw.like(StringUtils.isNotBlank(query.getReqIp()), OaiReqLog::getReqIp, query.getReqIp());
        lqw.like(StringUtils.isNotBlank(query.getReqAddress()), OaiReqLog::getReqAddress, query.getReqAddress());
        lqw.eq(query.getReqStatus() != null, OaiReqLog::getReqStatus, query.getReqStatus());
        return lqw;
    }
}
